package com.springdoor.wx.api.controller;

import com.springdoor.wx.api.entity.SprdGoodsBuyCart;
import com.springdoor.wx.api.service.SprdGoodsBuyCartService;
import com.springdoor.wx.core.controller.BaseController;
import com.springdoor.wx.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RequestMapping("buyCart")
@RestController
public class SprdGoodsBuyCartController extends BaseController {

    @Autowired
    private SprdGoodsBuyCartService buyCartService;


    @RequestMapping("findList")
    public AjaxResult findListByUserId() {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        return success(buyCartService.findListByUserId(userId));
    }

    @RequestMapping("findBuyCartNum")
    public AjaxResult findBuyCartNum() {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        return success(buyCartService.findBuyCartNum(userId));
    }

    @PostMapping("add")
    public AjaxResult insertSprdGoodsBuyCart(@RequestBody SprdGoodsBuyCart buyCart) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        buyCart.setUserId(userId);
        buyCart.setCreateBy(getUsername());
        buyCart.setUpdateBy(getUsername());
        return toAjax(buyCartService.insertSprdGoodsBuyCart(buyCart));
    }

    @PostMapping("update")
    public AjaxResult updateSprdGoodsBuyCart(@RequestBody SprdGoodsBuyCart buyCart) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        buyCart.setUserId(userId);
        buyCart.setCreateBy(getUsername());
        buyCart.setUpdateBy(getUsername());
        return toAjax(buyCartService.updateSprdGoodsBuyCart(buyCart));
    }

    @PostMapping("cancel")
    public AjaxResult deleteSprdGoodsBuyCartByUserId(@RequestBody SprdGoodsBuyCart param) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        SprdGoodsBuyCart buyCart = new SprdGoodsBuyCart();
        buyCart.setUserId(userId);
        buyCart.setUpdateBy(getUsername());
        buyCart.setDelFlag("1");
        buyCart.setSkuId(param.getSkuId());
        buyCart.setRemark(param.getRemark());
        return toAjax(buyCartService.updateBuyCartStatus(buyCart));
    }

    @PostMapping("updateStoreSelStatus")
    public AjaxResult updateStoreSelStatus(@RequestBody Map<String, Object> param) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        SprdGoodsBuyCart buyCart = new SprdGoodsBuyCart();
        buyCart.setUserId(userId);
        buyCart.setUpdateBy(getUsername());
        buyCart.setDeptId(Long.valueOf(param.get("storeId").toString()));
        buyCart.setIsSelected(param.get("isSelected").toString());
        buyCart.setRemark(param.get("remark").toString());
        return toAjax(buyCartService.updateStoreSelStatus(buyCart));
    }

    @PostMapping("updateGoodsSelStatus")
    public AjaxResult updateGoodsSelStatus(@RequestBody SprdGoodsBuyCart buyCart) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        buyCart.setUserId(userId);
        buyCart.setUpdateBy(getUsername());
        return toAjax(buyCartService.updateGoodsSelStatus(buyCart));
    }

    @PostMapping("updateAllGoodsSelStatus")
    public AjaxResult updateAllGoodsSelStatus(@RequestBody SprdGoodsBuyCart buyCart) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        buyCart.setUserId(userId);
        buyCart.setUpdateBy(getUsername());
        return toAjax(buyCartService.updateAllGoodsSelStatus(buyCart));
    }

    @PostMapping("updateGoodsQuantity")
    public AjaxResult updateGoodsQuantity(@RequestBody SprdGoodsBuyCart buyCart) {
        Long userId = getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        buyCart.setUserId(userId);
        buyCart.setUpdateBy(getUsername());
        return toAjax(buyCartService.updateGoodsQuantity(buyCart));
    }

}
